package com.example.iqiyitest.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;
import android.view.View;

public class CustomGridLayoutManager extends GridLayoutManager {

	private int spanCount;
	public CustomGridLayoutManager(Context context, int spanCount) {
		super(context, spanCount);
		this.spanCount = spanCount;
	}

	public CustomGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	public CustomGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
		super(context, spanCount, orientation, reverseLayout);
		this.spanCount = spanCount;
	}

	@Override
	public View onInterceptFocusSearch(View focused, int direction) {
		if (direction == View.FOCUS_DOWN) {
			int pos = getPosition(focused);
			int size = getChildCount();
			int count = getItemCount();
			if (size > 0) {
				int startIndex = 0;
				if (size >= spanCount) {
					startIndex = size - spanCount;
				}
				View view;
				for (int i = startIndex; i < size; i++) {
					view = getChildAt(i);
					if (view == focused) {
						int lastVisibleItemPos = findLastCompletelyVisibleItemPosition();
						if (pos > lastVisibleItemPos) { 
							return focused;
						} else {
							int lastLineStartIndex = 0;
							if (count >= spanCount) {
								lastLineStartIndex = count - ((count % spanCount) == 0 ? spanCount : (count % spanCount));
							}
							if (pos >= lastLineStartIndex && pos < count) { // 最后一排的可见view时,返回当前view
								return focused;
							}
							if(lastLineVisibleSearchDown!=null){
								lastLineVisibleSearchDown.searchDown();
								return focused;
							}
							break;
						}
					}
				}
			} else {
				return focused;
			}
		}else if(direction == View.FOCUS_RIGHT){
			int pos = getPosition(focused);
			if ((pos + 1) % spanCount == 0) {
				int size = getChildCount();
				int count = getItemCount();
				System.out.println(pos + "/" + size + "/" + count);
				View view = null;
				for (int i = 0; i < size; i++) {
					view = getChildAt(i);
					if (view == focused) {
						if((i+1)<count){
							return getChildAt(i+1);
						}
						break;
					}
				}
			}
		}
		return super.onInterceptFocusSearch(focused, direction);
	}
	
	public View getNextDownView(View focused){
		int pos = getPosition(focused);
		int size = getChildCount();
		int count = getItemCount();
		if (size > 0) {
			View view;
			for (int i = 0; i < size; i++) {
				view = getChildAt(i);
				if (view == focused) {
					int lastVisibleItemPos = findLastCompletelyVisibleItemPosition();
					if (pos > lastVisibleItemPos) { 
						return focused;
					} else {
						int lastLineStartIndex = 0;
						if (count >= spanCount) {
							lastLineStartIndex = count - ((count % spanCount) == 0 ? spanCount : (count % spanCount));
						}
						if (pos >= lastLineStartIndex && pos < count) { // 最后一排的可见view时,返回当前view
							return focused;
						}
						try {
							if ((i + spanCount + 1) < size) {
									View v = getChildAt(i + spanCount);
									if (v != null) {
										return v;
									}
							}else if(i<(size-1)){
									View v = getChildAt(size-1);
									if (v != null) {
										return v;
									}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					}
				}
			}
		}
		return super.onInterceptFocusSearch(focused, View.FOCUS_DOWN);

	}
	
	private OnLastLineVisibleSearchDown lastLineVisibleSearchDown;
	
	public void setLastLineVisibleSearchDown(OnLastLineVisibleSearchDown lastLineVisibleSearchDown) {
		this.lastLineVisibleSearchDown = lastLineVisibleSearchDown;
	}
	
	public interface OnLastLineVisibleSearchDown{
		public void searchDown();
	}
	
	public List<MovieView> getDisplayedViews() {
		List<MovieView> views = new ArrayList<MovieView>();
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			View view = getChildAt(i);
			if(view instanceof MovieView){
				views.add((MovieView) view);
			}else{
				continue;
			}
		}
		return views;
	}
}
